package net.m510.autosurfer;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.m510.autosurfer.common.cli.OutDirOption;
import net.m510.autosurfer.common.cli.PasswordOption;
import net.m510.autosurfer.core.AbstractMain;
import net.m510.autosurfer.core.exception.UserProcessException;
import net.m510.autosurfer.core.log.LogUtils;
import net.m510.autosurfer.idemitsu.IdemitsuDownload;
import net.m510.autosurfer.idemitsu.IdemitsuGoBillDetail;
import net.m510.autosurfer.idemitsu.IdemitsuGoSpendingDetail;
import net.m510.autosurfer.idemitsu.IdemitsuGoTop;
import net.m510.autosurfer.idemitsu.IdemitsuLogin;
import net.m510.autosurfer.idemitsu.IdemitsuLogout;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.jsoup.nodes.Document;


public class IdemitsuGetMain extends AbstractMain {
	private static final Logger log = Logger.getLogger(IdemitsuGetMain.class.getName());
	private static final String DEFAULT_MEISAI_DIRNAME=".";
	
	private String userId;
	private String password;
	private File outDir;
	/**
	 * 
	 */
	public IdemitsuGetMain() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		IdemitsuGetMain main = new IdemitsuGetMain();
		main.setLogger(log);
		main.Main(args);
	}
	
	@Override
	protected Document start() throws IOException, UserProcessException {
		IdemitsuGoTop p = new IdemitsuGoTop(client,context);
		Document out = p.process(null);
		return out;
	}

	@Override
	protected Document login(Document doc) throws IOException, UserProcessException {
		IdemitsuLogin p = new IdemitsuLogin(client,context);
		p.setUserId(userId);
		p.setPassword(password);
		Document out = p.process(doc);
		p.check(out);
		
		log.fine("doc="+out.html());
		return out;
	}

	@Override
	protected Document prepareDownload(Document doc) throws IOException, UserProcessException {
		log.info("ご利用明細照会ページへ遷移開始");
		
		IdemitsuGoSpendingDetail spendp = new IdemitsuGoSpendingDetail(client,context);
		Document out1 = spendp.process(doc);
		log.info("ご利用明細照会ページへ遷移終了");
		
		log.info("ご請求金額詳細ページへ遷移開始");
		IdemitsuGoBillDetail billp = new IdemitsuGoBillDetail(client,context);
		Document out2 = billp.process(out1);
		LogUtils.logElements(log, Level.FINE, out2.getElementsByTag("a"),"anchors");
		log.info("ご請求金額詳細ページへ遷移終了");
		
		return out2;
	}

	@Override
	protected File download(Document doc) throws IOException, UserProcessException {
		IdemitsuDownload p = new IdemitsuDownload(client,context,outDir);
		File f = p.process(doc);
		return f;
	}

	@Override
	protected void logout(Document doc) throws IOException, UserProcessException {
		IdemitsuLogout p = new IdemitsuLogout(client,context);
		p.process(doc);
	}
	
	@SuppressWarnings("static-access")
	@Override
	protected Options getOptions() {
		Options options = new Options();
		Option out = new OutDirOption(false);options.addOption(out);
		options.addOption(OptionBuilder.hasArg(true).withArgName("login id")
				.isRequired(true).withDescription("ログインID")
				.create("id"));
		Option password = new PasswordOption(true); options.addOption(password);
		return options;
	}

	@Override
	protected void setParams(CommandLine cmd) {
		userId = cmd.getOptionValue("id");
		this.password = cmd.getOptionValue(PasswordOption.OPT);
		outDir = new File(cmd.getOptionValue(OutDirOption.OPT, DEFAULT_MEISAI_DIRNAME));
	}

}
